﻿namespace OP10.ViewModels
{
    public class ST04LeftMotorOperateViewModel : BindableBase
    {
        private readonly IRegionManager _regionManager;

        public ST04LeftMotorOperateViewModel(IRegionManager regionManager)
        {
            _regionManager = regionManager;

            InitMotorParam();

            //开启定时器任务
            DispatcherTimer dispatcherTimer = new DispatcherTimer();
            dispatcherTimer.Interval = TimeSpan.FromMilliseconds(200);
            dispatcherTimer.Tick += OnTick;
            dispatcherTimer.Start();

        }
        #region 私有方法
        //初始化电机参数
        private void InitMotorParam()
        {
            Speed = (float)LeftMotorParam_Global.Speed/10000;
            WorkPosition = (float)LeftMotorParam_Global.WorkPosition/10000;
            PositionError = (float)LeftMotorParam_Global.Position_Error/10000;
            VM_SPEED = (float)LeftMotorParam_Global.VM_SPEED/10000;
            VM_ACC = (float)LeftMotorParam_Global.VM_ACC/10000;
            DirectPos = (float)LeftMotorParam_Global.DirectPos/10000;
            DirectSpeed = (float)LeftMotorParam_Global.DirectSpeed/10000;
            SafeLocation= (float)LeftMotorParam_Global.SafeLocation/10000;
        }

        private void OnTick(object? sender, EventArgs e)
        {
            ReadyOK=LeftMotorPLCToPC_Global.ReadyOK;
            AtHome=LeftMotorPLCToPC_Global.atHome;
            ReceiveString=LeftMotorPLCToPC_Global.ReceiveString;
            GetActualPos=LeftMotorPLCToPC_Global.ActutalPositon/10000;
            GetActualPower=LeftMotorPLCToPC_Global.ActutalPression;


        }
        #endregion

        #region 事件
        private DelegateCommand _GoBackCommand;

        public DelegateCommand GoBackCommand =>
            _GoBackCommand ?? (_GoBackCommand = new DelegateCommand(ExecuteGoBackCommand));

        void ExecuteGoBackCommand()
        {
            _regionManager.RequestNavigate(PrismManager.MainViewRegionName, AppViews.ST04DeviceManagerView);
        }



        /// <summary>
        /// 获取当前位置
        /// </summary>
        private DelegateCommand _GetActualPosDownCmd;
        public DelegateCommand GetActualPosDownCmd =>
            _GetActualPosDownCmd ?? (_GetActualPosDownCmd = new DelegateCommand(ExecuteGetActualPosDownCmd));

        void ExecuteGetActualPosDownCmd()
        {
            LeftMotorPCToPLC_Global.GetActualPos = true;
        }

        private DelegateCommand _GetActualPosUpCmd;
        public DelegateCommand GetActualPosUpCmd =>
            _GetActualPosUpCmd ?? (_GetActualPosUpCmd = new DelegateCommand(ExecuteGetActualPosUpCmd));

        void ExecuteGetActualPosUpCmd()
        {
            LeftMotorPCToPLC_Global.GetActualPos = false;
        }
        /// <summary>
        /// 获取当前力
        /// </summary>
        private DelegateCommand _GetActualPowerDownCmd;
        public DelegateCommand GetActualPowerDownCmd =>
            _GetActualPowerDownCmd ?? (_GetActualPowerDownCmd = new DelegateCommand(ExecuteGetActualPowerDownCmd));

        void ExecuteGetActualPowerDownCmd()
        {
            LeftMotorPCToPLC_Global.GetActualPress = true;
        }

        private DelegateCommand _GetActualPowerUpCmd;
        public DelegateCommand GetActualPowerUpCmd =>
            _GetActualPowerUpCmd ?? (_GetActualPowerUpCmd = new DelegateCommand(ExecuteGetActualPowerUpCmd));

        void ExecuteGetActualPowerUpCmd()
        {
            LeftMotorPCToPLC_Global.GetActualPress = false;

        }

        private DelegateCommand _MoveDownCmd;
        public DelegateCommand MoveDownCmd =>
            _MoveDownCmd ?? (_MoveDownCmd = new DelegateCommand(ExecuteMoveDownCmd));

        void ExecuteMoveDownCmd()
        {
            LeftMotorPCToPLC_Global.Move = true;
        }

        private DelegateCommand _MoveUpCmd;
        public DelegateCommand MoveUpCmd =>
            _MoveUpCmd ?? (_MoveUpCmd = new DelegateCommand(ExecuteMoveUpCmd));

        void ExecuteMoveUpCmd()
        {
            LeftMotorPCToPLC_Global.Move = false;
        }


        private DelegateCommand _DirectGoDownCmd;
        public DelegateCommand DirectGoDownCmd =>
            _DirectGoDownCmd ?? (_DirectGoDownCmd = new DelegateCommand(ExecuteDirectGoDownCmd));

        void ExecuteDirectGoDownCmd()
        {

            LeftMotorPCToPLC_Global.DirectGo = true;

        }

        private DelegateCommand _DirectGoUpCmd;
        public DelegateCommand DirectGoUpCmd =>
            _DirectGoUpCmd ?? (_DirectGoUpCmd = new DelegateCommand(ExecuteDirectGoUpCmd));

        void ExecuteDirectGoUpCmd()
        {
            LeftMotorPCToPLC_Global.DirectGo = false;

        }


        private DelegateCommand _InitialDownCmd;
        public DelegateCommand InitialDownCmd =>
            _InitialDownCmd ?? (_InitialDownCmd = new DelegateCommand(ExecuteInitialDownCmd));

        void ExecuteInitialDownCmd()
        {
            LeftMotorPCToPLC_Global.Initial = true;

        }

        private DelegateCommand _InitialUpCmd;
        public DelegateCommand InitialUpCmd =>
            _InitialUpCmd ?? (_InitialUpCmd = new DelegateCommand(ExecuteInitialUpCmd));

        void ExecuteInitialUpCmd()
        {
            LeftMotorPCToPLC_Global.Initial = false;

        }

        private DelegateCommand _GoHomeDownCmd;
        public DelegateCommand GoHomeDownCmd =>
            _GoHomeDownCmd ?? (_GoHomeDownCmd = new DelegateCommand(ExecuteGoHomeDownCmd));

        void ExecuteGoHomeDownCmd()
        {
            LeftMotorPCToPLC_Global.GoHome = true;
        }

        private DelegateCommand _GoHomeUpCmd;
        public DelegateCommand GoHomeUpCmd =>
            _GoHomeUpCmd ?? (_GoHomeUpCmd = new DelegateCommand(ExecuteGoHomeUpCmd));

        void ExecuteGoHomeUpCmd()
        {
            LeftMotorPCToPLC_Global.GoHome = false;
        }

        private DelegateCommand _MotorOffDownCmd;
        public DelegateCommand MotorOffDownCmd =>
            _MotorOffDownCmd ?? (_MotorOffDownCmd = new DelegateCommand(ExecuteMotorOffDownCmd));

        void ExecuteMotorOffDownCmd()
        {
            LeftMotorPCToPLC_Global.MotorOFF = true;
        }


        private DelegateCommand _MotorOffUpCmd;
        public DelegateCommand MotorOffUpCmd =>
            _MotorOffUpCmd ?? (_MotorOffUpCmd = new DelegateCommand(ExecuteMotorOffUpCmd));

        void ExecuteMotorOffUpCmd()
        {
            LeftMotorPCToPLC_Global.MotorOFF = false;

        }


        private DelegateCommand<string> _TextChangeCommand;
        public DelegateCommand<string> TextChangeCommand =>
            _TextChangeCommand ?? (_TextChangeCommand = new DelegateCommand<string>(ExecuteTextChangeCommand));

        void ExecuteTextChangeCommand(string p)
        {
            switch (p)
            {

                case "Speed":
                    LeftMotorParam_Global.Speed = Convert.ToInt32 (Speed*10000);
                    break;

                case "WorkPosition":
                    LeftMotorParam_Global.WorkPosition = Convert.ToInt32(WorkPosition * 10000);
                    break;

                case "PositionError":

                    LeftMotorParam_Global.Position_Error = Convert.ToInt32(PositionError * 10000);
                    break;

                case "VM_SPEED":

                    LeftMotorParam_Global.VM_SPEED = Convert.ToInt32(VM_SPEED * 10000);
                    break;

                case "VM_ACC":
                    LeftMotorParam_Global.VM_ACC= Convert.ToInt32(VM_ACC * 10000);

                    break;

                case "DirectPos":
                    LeftMotorParam_Global.DirectPos = Convert.ToInt32(DirectPos * 10000);
                    break;

                case "DirectSpeed":
                    LeftMotorParam_Global.DirectSpeed = Convert.ToInt32(DirectSpeed * 10000);
                    break;
                case "SafeLocation":
                    LeftMotorParam_Global.SafeLocation= Convert.ToInt32(SafeLocation * 10000);
                    break;
                default:
                    break;
            }
        }

        private DelegateCommand _NavigateRightCmd;
        public DelegateCommand NavigateRightCmd =>
            _NavigateRightCmd ?? (_NavigateRightCmd = new DelegateCommand(ExecuteNavigateRightCmd));

        void ExecuteNavigateRightCmd()
        {
            _regionManager.RequestNavigate(PrismManager.MainViewRegionName, AppViews.ST04RightMotorOperateView);
        }
        #endregion


        #region 属性

        private bool _ReadyOK;
        public bool ReadyOK
        {
            get { return _ReadyOK; }
            set { SetProperty(ref _ReadyOK, value); }
        }

        private bool _AtHome;
        public bool AtHome
        {
            get { return _AtHome; }
            set { SetProperty(ref _AtHome, value); }
        }

        private float _Speed;
        public float Speed
        {
            get { return _Speed; }
            set { SetProperty(ref _Speed, value); }
        }

        private float _WorkPosition;
        public float WorkPosition
        {
            get { return _WorkPosition; }
            set { SetProperty(ref _WorkPosition, value); }
        }


        private float _PositionError;
        public float PositionError
        {
            get { return _PositionError; }
            set { SetProperty(ref _PositionError, value); }
        }

        private float _VM_SPEED;
        public float VM_SPEED
        {
            get { return _VM_SPEED; }
            set { SetProperty(ref _VM_SPEED, value); }
        }

        private float _VM_ACC;
        public float VM_ACC
        {
            get { return _VM_ACC; }
            set { SetProperty(ref _VM_ACC, value); }
        }

        private float _GetActualPos;
        public float GetActualPos
        {
            get { return _GetActualPos; }
            set { SetProperty(ref _GetActualPos, value); }
        }

        private float _GetActualPower;
        public float GetActualPower
        {
            get { return _GetActualPower; }
            set { SetProperty(ref _GetActualPower, value); }
        }

        private float _DirectPos;
        public float DirectPos
        {
            get { return _DirectPos; }
            set { SetProperty(ref _DirectPos, value); }
        }

        private float _DirectSpeed;
        public float DirectSpeed
        {
            get { return _DirectSpeed; }
            set { SetProperty(ref _DirectSpeed, value); }
        }


        private string _ReceiveString;
        public string ReceiveString
        {
            get { return _ReceiveString; }
            set { SetProperty(ref _ReceiveString, value); }
        }


        private float _SafeLocation;
        public float SafeLocation
        {
            get { return _SafeLocation; }
            set { SetProperty(ref _SafeLocation, value); }
        }
        #endregion
    }
}
